﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml.Serialization;
using Editor.Utilities;
using System.Threading;
using System.Windows.Forms;

namespace Editor.Elements
{
    [XmlInclude(typeof(Department))]
    public class Project : Element
    {
        private List<Department> departments = new List<Department>();
        private string path;
        private Thread saveThread;
        
        public Project() 
        {
        }

        public Project(SDictionary<string> dictionary)
            : base(dictionary)
        {
        }

        public override IList ChildElements
        {
            get
            {
                return departments;
            }
            set
            {
                departments = value as List<Department>;
            }
        }

        [XmlIgnore]
        public bool IsSaved { get; set; }

        [XmlIgnore]
        public bool IsLoaded { get; set; }

        public override Element CreateChildElement(SDictionary<string> dictionary)
        {
            return new Department(dictionary);
        }

        public override void AddChildElement(Element element)
        {
            base.AddChildElement(element);
            IsSaved = false;
        }

        public override Element AddChildElement(SDictionary<string> dictionary)
        {
            Element elem = base.AddChildElement(dictionary);
            
            IsSaved = false;

            return elem;
        }

        public override void RemoveChildElement(Element element)
        {
            base.RemoveChildElement(element);
            IsSaved = false;
        }

        /*
        public override void RemoveChildElement(string elementName)
        {
            base.RemoveChildElement(elementName);
            IsSaved = false;
        }
         * */
        
        public static Project Load(string fileName)
        {
            Project loadedProject = XMLWorker<Project>.DeserializeFromXML(fileName);

            loadedProject.path = fileName;
            loadedProject.UpdateParents();
            loadedProject.IsLoaded = true;
            loadedProject.IsSaved = true;

            return loadedProject; 
        }

        private void saveThreadMethod()
        {
            if (path == null)
                path = Dictionary[EditorForm.DefaultLanguage] + ".proj";

            XMLWorker<Project>.SerializeToXML(this, path);
            IsSaved = true;
        }

        public void Save()
        {
            if (saveThread != null && saveThread.ThreadState == ThreadState.Running)
            {
                MessageBox.Show("Başka bir kaydetme devam ediyor.. Tekrar deneyin!", "Hata", 
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                saveThread = new Thread(new ThreadStart(this.saveThreadMethod));
                saveThread.Start();
            }
        }

        public void UpdateParents()
        {
            updateParent(this);
        }

        private void updateParent(Element element)
        {
            foreach (Element child in element.ChildElements)
            {
                child.Parent = element;
                updateParent(child);
            }
        }
    }
}
